package com.uci.photogam.facebook;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;

import com.uci.photogam.R;

public class FacebookAlbumGallery extends Activity implements OnItemClickListener{
	protected static JSONArray jsonImageArray;
	//private Gallery gallery;
	private GridView grid;
	private int size = 130; // size of the image width x height
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fb_album_pictures);
		//gallery = (Gallery)this.findViewById(R.id.fb_album_images);
		grid = (GridView)this.findViewById(R.id.fb_album_images_gridview);
		Bundle extras = getIntent().getExtras();
		String apiResponse = extras.getString("API_RESPONSE");
		try {
			jsonImageArray = new JSONObject(apiResponse).getJSONArray("data");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		gallery.setOnItemClickListener(this);
//		gallery.setAdapter(new ImageAdapter(this.getBaseContext()));
		grid.setOnItemClickListener(this);
		grid.setAdapter(new ImageAdapter(this.getBaseContext()));
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		// TODO Auto-generated method stub
		try {
			JSONObject jsonObject = jsonImageArray.getJSONObject(position);
			Bitmap image = Utility.getBitmap(jsonObject.getString("source"));
			Bundle bundle = new Bundle();
			bundle.putParcelable("image", image);
			bundle.putString("imgUrl", jsonObject.getString("source"));
			Intent intent = new Intent();
			intent.putExtras(bundle);
			FacebookAlbumGallery.this.setResult(RESULT_OK, intent);
			FacebookAlbumGallery.this.finish();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private class ImageAdapter extends BaseAdapter{
		private Context context;

		public ImageAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return jsonImageArray.length();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView iv = new ImageView(context);
			JSONObject jsonObject = null;
			try {
				jsonObject = jsonImageArray.getJSONObject(position);
				JSONArray imageSizesArray = jsonObject.getJSONArray("images");
				for(int i = 0; i < imageSizesArray.length(); i++)
				{
					if(imageSizesArray.getJSONObject(i).getInt("width") >= 180)
						jsonObject = imageSizesArray.getJSONObject(i);
					else
						break;	
				}
				//jsonObject = imageSizesArray.getJSONObject(imageSizesArray.length()-1);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			
			iv.setLayoutParams(new GridView.LayoutParams(size, size));
	        //iv.setScaleType(ImageView.ScaleType.FIT_XY);
	        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
			//iv.setPadding(5, 10, 5, 10);
			
			Bitmap image;
			try{
				image = Utility.getBitmap(jsonObject.getString("source"));
		        iv.setImageBitmap(image);
			//	iv.setLayoutParams(new Gallery.LayoutParams(image.getWidth(),400));
			} catch(JSONException e)
			{
				//iv.setLayoutParams(new Gallery.LayoutParams(iv.getWidth(),400));
		        iv.setImageResource(R.drawable.ic_launcher);
				Log.e("AlbumImages", "Image not found");
			}
			
			iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			
			return iv;
		}

	}
}
